<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_routeLocatorLine"></title>
    <script type="text/javascript" src="../js/include-web.js"></script>
    <script type="text/javascript" src="../../dist/ol/include-ol.js"></script>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%; position: absolute;top: 0;">
<div id="map" style="width: 100%;height:100%"></div>
<script type="text/javascript">
    var map, options, queryBySQLParams, queryBySQLService, resultLayer,
        routeLocatorParameters_line, routeLocatorService,
        baseUrl = (window.isLocal ? window.server : "https://iserver.supermap.io")+"/iserver/services/map-changchun/rest/maps/长春市区图",
        serviceUrl = (window.isLocal ? window.server : "https://iserver.supermap.io")+"/iserver/services/spatialanalyst-changchun/restjsr/spatialanalyst";
    var extent = [48.4, -7668.25, 8958.85, -55.58];
    var projection = new ol.proj.Projection({
        code:'',
        extent: extent,
        units: 'm'
    });
    var mapService = new ol.supermap.MapService(baseUrl);
    mapService.getMapInfo(function (serviceResult) {
        var mapJSONObj = serviceResult.result;
        map = new ol.Map({
            target: 'map',
            // ol v7版本用法为ol.control.defaults.defaults； v6版本以下用法为ol.control.defaults
            controls: ol.control.defaults.defaults({attributionOptions: {collapsed: false}})
                .extend([new ol.supermap.control.Logo({ link: "https://iclient.supermap.io" })]),
            view: new ol.View({
                center: [4503.62, -3861.91],
                zoom: 2,
                projection: projection,
                multiWorld: true
            })
        });
        options = ol.source.TileSuperMapRest.optionsFromMapJSON(baseUrl, mapJSONObj);
        var layer = new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest(options)
        });
        map.addLayer(layer);
        routeLocatorProcess_line();
    });

    function routeLocatorProcess_line() {
        //通过SQL查询的方法建立路由,并添加到地图上
        queryBySQLService = new ol.supermap.QueryService(baseUrl);
        queryBySQLParams = new ol.supermap.QueryBySQLParameters({
            queryParams: [
                new ol.supermap.FilterParameter({
                    name: "RouteDT_road@Changchun",
                    attributeFilter: "RouteID=1690"
                })
            ]
        });
        queryBySQLService.queryBySQL(queryBySQLParams, function (SQLQueryServiceResult) {
            var queryBySQLResult = SQLQueryServiceResult.result.recordsets[0].features;
            var pathSource = new ol.source.Vector({
                features: (new ol.format.GeoJSON()).readFeatures(queryBySQLResult)
            });
            pathLayer = new ol.layer.Vector({
                source: pathSource,
                style: new ol.style.Style({
                    stroke: new ol.style.Stroke({
                        color: 'rgba(100, 100, 225, 10)',
                        width: 3
                    })
                })
            });
            map.addLayer(pathLayer);

            //将形成路由的点提出来，为了构造下面点定里程服务sourceRoute
            var pointsList = [];
            var routeObj = queryBySQLResult.features[0].geometry.coordinates[0];
            for (var i = 0; i < routeObj.length; i++) {
                pointsList.push([routeObj[i][0], routeObj[i][1], routeObj[i][2]])
            }
            var routeLine = new ol.geom.LineString([pointsList]);

            //里程定线服务
            routeLocatorService = new ol.supermap.SpatialAnalystService(serviceUrl);
            routeLocatorParameters_line = new ol.supermap.RouteLocatorParameters({
                "sourceRoute": routeLine,
                "type": "LINE",
                "startMeasure": 200,
                "endMeasure": 1000,
                "isIgnoreGap": true
            });
            routeLocatorService.routeLocate(routeLocatorParameters_line).then(function (routeLocateServiceResult) {
                var vectorSource = new ol.source.Vector({
                    features: [(new ol.format.GeoJSON()).readFeature(routeLocateServiceResult.result.resultGeometry)],
                    wrapX: false
                });
                resultLayer = new ol.layer.Vector({
                    source: vectorSource,
                    style: new ol.style.Style({
                        stroke: new ol.style.Stroke({
                            color: 'red',
                            width: 3
                        }),
                        fill: new ol.style.Fill({
                            color: 'rgba(0, 0, 255, 0.1)'
                        })
                    })
                });
                map.addLayer(resultLayer);
            });
        });
    }
</script>
</body>
</html>